package com.seuic.hnjiningposapp.hnapps.ui.view.dialog;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.core.BasePopupView;
import com.seuic.hnjiningposapp.R;
import com.seuic.hnjiningposapp.hnapps.util.HttpUtilsV2;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 输入 交易类型  卡号收款人姓名  金额
 */
public class Input_nunname_moneyPp extends BasePopupView {
    Unbinder bind;

    @BindView(R.id.input_rnum_edtext)
    EditText inputRnumEdtext;
    @BindView(R.id.inputnum_rname_edtext)
    EditText inputnumRnameEdtext;
    @BindView(R.id.inputnum_rmoney_edtext)
    EditText inputnumRmoneyEdtext;
    @BindView(R.id.input_gzbtn)
    Button inputGzbtn;
    @BindView(R.id.input_numokbtn)
    Button inputNumokbtn;
    @BindView(R.id.pay_type)
    TextView payType;
    Context mContext;
    String type;
    String card_num;
    String username;
    String money;
    onInputNMMCallBack mOnInputNMMCallBack;
    Activity mActivity;
    @BindView(R.id.input_num_layout)
    LinearLayout inputNumLayout;

    public TextView getPayType() {
        return payType;
    }

    public void setPayType(TextView payType) {
        this.payType = payType;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getMoney() {
        return money;
    }

    public void setMoney(String money) {
        this.money = money;
    }


    public Input_nunname_moneyPp(@NonNull Context context, Activity activity, onInputNMMCallBack onInputNMMCallBack) {
        super(context);
        this.mContext = context;
        this.mActivity = activity;
        this.mOnInputNMMCallBack = onInputNMMCallBack;
    }

    @Override
    protected int getPopupLayoutId() {
        return R.layout.input_trnum_layout;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        bind = ButterKnife.bind(this);
        inputGzbtn.setVisibility(View.GONE);

        payType.setText(type);
        inputNumLayout.setVisibility(VISIBLE);
        inputRnumEdtext.setOnEditorActionListener((v, actionId, event) -> {
            if (inputRnumEdtext.getText().toString().equals("")) {
                ToastUtils.showLong("请输入账号");
            } else {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    goSearch();
                }
            }
            return false;
        });

    }

    private void goSearch() {
        HttpUtilsV2.getQueryBankCardInfo(inputRnumEdtext.getText().toString().trim(), new HttpUtilsV2.NetCallBack() {
            @Override
            public void onSuccess(String ServerSeqNo, String ConsumerSeqNo, JSONObject rspBody) {
                mActivity.runOnUiThread(() -> inputnumRnameEdtext.setText(rspBody.getString("AcctName")));
            }

            @Override
            public void onError(String code, String msg) {
                ToastUtils.showLong(msg + "请手动输入");
            }
        });
    }

    @OnClick({R.id.input_gzbtn, R.id.input_numokbtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.input_gzbtn:
                break;
            case R.id.input_numokbtn:
                if (inputRnumEdtext.getText().toString().equals("")) {
                    ToastUtils.showLong("请输入转账卡号");
                    return;
                }
                if (inputnumRnameEdtext.getText().toString().equals("")) {
                    ToastUtils.showLong("请输入收款人姓名");
                    return;
                }
                if (inputnumRmoneyEdtext.getText().toString().equals("")) {
                    ToastUtils.showLong("请输入转账金额");
                    return;
                }
                mOnInputNMMCallBack.onNext(inputRnumEdtext.getText().toString(),
                        inputnumRnameEdtext.getText().toString(),
                        inputnumRmoneyEdtext.getText().toString());
                break;
        }
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
        //注解解绑
        if (bind != null && bind != Unbinder.EMPTY) {
            bind.unbind();
        }
        this.bind = null;
    }

    public interface onInputNMMCallBack {

        void onNext(String card_num, String user_name, String money);
    }
}
